<?php


namespace App\Libraries\AppSocialite\Providers;


use App\Exceptions\BusinessException;
use App\Libraries\AppSocialite\User;
use App\Libraries\VerifyPhoneNumber\Verifier;

class PhoneProvider extends AbstractProvider
{
    /**
     * @inheritDoc
     * @throws BusinessException
     */
    protected function getUserByToken($token)
    {
        if (!(new Verifier())->match($token, $this->request['code'])) {
            throw new BusinessException('短信验证码无效');
        }

        return [];
    }

    /**
     * @inheritDoc
     */
    protected function mapUserToObject(array $user)
    {
        return (new User())
            ->setRaw($user)
            ->map([
                'id' => $this->request['mobile'],
                'name' => $this->request['mobile'],
                'mobile' => $this->request['mobile'],
            ]);
    }

    /**
     * @inheritDoc
     */
    public function user()
    {
        $user = $this->getUserByToken($this->request['mobile']);

        return $this->mapUserToObject($user);
    }
}
